<template>
    <div>
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span>文章列表</span>
            </div>
            <!-- 搜索区域 -->
            <div class="search-box">
                <el-form :inline="true" :model="q">
                    <el-form-item label="文章分类">
                        <el-select v-model="q.cate_id" placeholder="请选择分类" size="small">
                            <el-option :label="item.cate_name" :value="item.id" v-for="(item, index) in cateList"
                                :key="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="发布状态" style="margin-left: 15px;">
                        <el-select v-model="q.state" placeholder="请选择状态" size="small">
                            <el-option label="已发布" value="已发布"></el-option>
                            <el-option label="草稿" value="草稿"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" size="small" @click="initArtList">筛选</el-button>
                        <el-button type="info" size="small" @click="reset">重置</el-button>
                    </el-form-item>
                </el-form>
                <!-- 发表文章的按钮 -->
                <el-button type="primary" size="small" class="btn-pub" @click="push">发表文章</el-button>

            </div>
            <!-- 文章详情 -->
            <el-dialog title="文章预览" :visible.sync="isShow" width="80%">
                <h1 class="title">{{ artWenzhang.title }} </h1>
                <div class="info">
                    <span>作者：{{ artWenzhang.nickname || artWenzhang.username }}</span>
                    <span>发布时间：{{ fn(artWenzhang.pub_date) }}</span>
                    <span>所属分类：{{ artWenzhang.cate_name }}</span>
                    <span>状态：{{ artWenzhang.state }}</span>
                </div>
                <el-divider></el-divider>
                <img :src="'http://big-event-vue-api-t.itheima.net' + artWenzhang.cover_img" alt="">
                <div v-html="artWenzhang.content"></div>
            </el-dialog>
            <!-- 文章表格区域 -->
            <el-table :data="artList" style="width: 100%;" border stripe>
                <el-table-column label="文章标题" prop="title">
                    <template v-slot="{ row }">
                        <el-link type="primary" @click="jump(row.id)">{{ row.title }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="分类" prop="cate_name"></el-table-column>
                <el-table-column label="发表时间" prop="pub_date">
                    <template v-slot="{ row }">
                        {{ fn(row.pub_date) }}
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="state"></el-table-column>
                <el-table-column label="操作">
                    <template v-slot="{ row }">
                        <el-button type="danger" size="mini" @click="del(row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <div class="block">
                <span class="demonstration">完整功能</span>
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="q.pagenum" :page-sizes="[2, 5, 10]" :page-size="q.pagesize"
                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import dayjs from '_dayjs@1.11.3@dayjs';
export default {
    name: 'ArtList',
    data() {
        return {
            // 查询参数对象
            q: {
                pagenum: 1,
                pagesize: 2,
                cate_id: '',
                state: ''
            },
            cateList: [],
            artList: [],
            artWenzhang: [],
            total: 0,
            isShow: false,
        }
    },
    methods: {
        push() {
            this.$router.push('/art-add');
        },
        async initArtList() {
            const { data: res } = await this.$http.get('/my/article/list', { params: this.q })
            // console.log(res);
            if (res.code != 0) return this.$message.error(res.message)
            this.artList = res.data
            this.total = res.total
        },
        handleSizeChange(val) {
            this.q.pagesize = val
            this.pagenum = 1
            this.initArtList()
        },
        handleCurrentChange(val) {
            this.q.pagenum = val
            this.initArtList()
        },
        async initCateList() {
            const { data: res } = await this.$http.get('/my/cate/list')
            if (res.code == 0) this.cateList = res.data
        },
        reset() {
            this.q = {
                pagenum: 1,
                pagesize: 2,
                cate_id: '',
                state: ''
            }
            this.initArtList()
        },
        async jump(id) {
            const { data: res } = await this.$http.get('/my/article/info', { params: { id } })
            if (res.code != 0) return this.$message.error('获取文章失败')
            this.artWenzhang = res.data
            // console.log(this.artWenzhang);
            this.isShow = true
        },
        async del(id) {
            const a = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
            if (a) {
                const { data: res } = await this.$http.delete('/my/article/info', { params: { id } })
                // console.log(res);
                if (res.code != 0) return this.$message.error(res.message)
                this.$message.success(res.message)
                this.initArtList()
            }
        },
        fn(val){
            return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
        }
    },
    created() {
        this.initCateList()
        this.initArtList()
    },
}
</script>

<style lang="less" scoped>
.search-box {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;

    .btn-pub {
        margin-top: 5px;
    }
}

.el-pagination {
    margin-top: 15px;
}

.title {
    font-size: 24px;
    text-align: center;
    font-weight: normal;
    color: #000;
    margin: 0 0 10px 0;
}

.info {
    font-size: 12px;

    span {
        margin-right: 20px;
    }
}
</style>
